package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.DegreeSubscribe;
import com.yuyou.fn.educational.mapper.DegreeSubscribeMapper;
import com.yuyou.fn.educational.service.IDegreeSubscribeService;
import com.yuyou.fn.educational.vo.CourseClassQueryVo;
import com.yuyou.fn.educational.vo.DegreeSubscribeDetailVo;
import com.yuyou.fn.educational.vo.DegreeSubscribeQueryVo;
import com.yuyou.fn.educational.vo.DegreeSubscribeSaveVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * DegreeSubscribe 表数据服务层接口实现类
 *
 */
@Service
public class DegreeSubscribeServiceImpl extends SuperServiceImpl<DegreeSubscribeMapper, DegreeSubscribe,Long> implements IDegreeSubscribeService {

    @Override
    public Page<DegreeSubscribeDetailVo> findDegreeSubscribeDetailPage(DegreeSubscribeQueryVo degreeSubscribeQueryVo, CourseClassQueryVo courseClassQueryVo, int pageNo, int pageSize) {
        if (degreeSubscribeQueryVo.getEndDate()!=null){
            degreeSubscribeQueryVo.setEndDate(TimeUtils.changeOneDateLastTime(degreeSubscribeQueryVo.getEndDate()));
        }
        Page<DegreeSubscribeDetailVo> page=new Page<>(pageNo,pageSize);
        List<DegreeSubscribeDetailVo> degreeSubscribeDetailVoList=this.autoMapper.findDegreeSubscribeList(degreeSubscribeQueryVo,courseClassQueryVo,(pageNo-1)*pageSize,pageSize);
        Integer total=this.autoMapper.countDegreeSubscribe(degreeSubscribeQueryVo,courseClassQueryVo);
        page.setRecords(degreeSubscribeDetailVoList);
        page.setTotal(total);
        return page;
    }

    @Override
    public void followDegreeSubscribe(long degreeSubscribeId) {
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("请登录");
        }
        DegreeSubscribe degreeSubscribe=new DegreeSubscribe();
        degreeSubscribe.setDegreeSubscribeId(degreeSubscribeId);
        degreeSubscribe.setFollowStatus(1);
        degreeSubscribe.setFollowName(principal.name);
        degreeSubscribe.setFollowId(principal.id);
        degreeSubscribe.setUpdateTime(TimeUtils.now());
        degreeSubscribe.setFollowTime(TimeUtils.now());
        this.updateSelectiveById(degreeSubscribe);
    }

    @Override
    public void saveDegreeSubscribe(DegreeSubscribeSaveVo degreeSubscribeSaveVo) {
        Example<DegreeSubscribe> example=DegreeSubscribe.createExample();
        example.createCriteria().andEqualTo("classId",degreeSubscribeSaveVo.getClassId()).andEqualTo("phoneNo",degreeSubscribeSaveVo.getPhoneNo());
        DegreeSubscribe check=this.findOneByExample(example);
        if (check!=null){
            throw new BusException("该手机号已经成功预约，请勿重复提交！");
        }
        DegreeSubscribe degreeSubscribe=new DegreeSubscribe();
        BeanUtils.copyProperties(degreeSubscribe,degreeSubscribeSaveVo);
        degreeSubscribe.setFollowStatus(0);
        degreeSubscribe.setCreateTime(TimeUtils.now());
        degreeSubscribe.setUpdateTime(TimeUtils.now());
        this.insertSelective(degreeSubscribe);
    }
}